﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MainMenuManager : MonoBehaviour {

	public List<GameObject> mainMenuBtns;
	[HideInInspector] public FileLoader fL;
	public MusicManager musicManager;
	public GameObject SettingsUI;
	public GameObject LoadUI;
	public GameObject MenuPanel;

	void Awake()
	{
		fL = new FileLoader();
		SettingsUI = GameObject.Find("SettingsUI");
		LoadUI = GameObject.Find("LoadUI");
		MenuPanel = GameObject.Find("MenuPanel");
		musicManager = new MusicManager();
		musicManager.musicManagerData = new MusicManagerData();
		musicManager.musicManagerData.BGM = gameObject.AddComponent<AudioSource>();
	}

	// Use this for initialization
	void Start () {
		musicManager.PlayBGM(Resources.Load("BGM/wit_diy", typeof(AudioClip)) as AudioClip);
		DeactivateOnStart();
	}

	void DeactivateOnStart()
	{
		SettingsUI.SetActive(false);
		LoadUI.SetActive(false);
		MenuPanel.SetActive(false);
	}
	
	// Update is called once per frame
	void Update () 
	{
		MainMenuButtonsUpdate();
		if(MenuPanel.activeSelf == true)
			MenuPanelButtonsUpdate();
	}

	#region Main Menu Button Functions

	void MainMenuButtonsUpdate()
	{
		mainMenuBtns[0].GetComponent<UIButtonMessage>().functionName = "onNewGame";
		mainMenuBtns[1].GetComponent<UIButtonMessage>().functionName = "onContinue";
		mainMenuBtns[2].GetComponent<UIButtonMessage>().functionName = "onLoad";
		mainMenuBtns[3].GetComponent<UIButtonMessage>().functionName = "onSettings";
		mainMenuBtns[4].GetComponent<UIButtonMessage>().functionName = "onQuit";
	}

	void onNewGame()
	{
		print ("load intro scene");
		Application.LoadLevel ("Pre-Introduction");

	}

	void onContinue()
	{
		fL.QuickLoad();
	}

	void onLoad()
	{
		print ("load");
		SettingsUI.SetActive(false);
		LoadUI.SetActive(true);
		if(!MenuPanel.activeSelf)
			MenuPanel.SetActive(true);

		for(int i = 0; i < mainMenuBtns.Count; i++)
			mainMenuBtns[i].SetActive(false);
	}

	void onSettings()
	{
		print ("settings");
		LoadUI.SetActive(false);
		SettingsUI.SetActive(true);
		if(!MenuPanel.activeSelf)
			MenuPanel.SetActive(true);

		for(int i = 0; i < mainMenuBtns.Count; i++)
			mainMenuBtns[i].SetActive(false);
	}

	void onQuit()
	{
		print ("quit");
		Application.Quit ();
	}
	#endregion

	#region Menu Panel Functions

	void MenuPanelButtonsUpdate()
	{
		MenuPanel.transform.FindChild("Cancel").GetComponent<UIButtonMessage>().functionName = "onCancel";
		MenuPanel.transform.FindChild("Load").GetComponent<UIButtonMessage>().functionName = "OnLoad";
		MenuPanel.transform.FindChild("Quickload").GetComponent<UIButtonMessage>().functionName = "onQuickload";
		MenuPanel.transform.FindChild("Settings").GetComponent<UIButtonMessage>().functionName = "OnSettings";
	}

	void onCancel()
	{
		SettingsUI.SetActive(false);
		LoadUI.SetActive(false);
		MenuPanel.SetActive(false);
		for(int i = 0; i < mainMenuBtns.Count; i++)
			mainMenuBtns[i].SetActive(true);
	}

	void OnLoad()
	{
		LoadUI.SetActive(true);
		SettingsUI.SetActive(false);
	}

	void onQuickload()
	{
		Debug.Log("Quick Loaded!");
		fL.QuickLoad();
	}

	void OnSettings()
	{
		LoadUI.SetActive(false);
		SettingsUI.SetActive(true);
	}

	#endregion

	#region Settings UI Functions



	#endregion
}
